//定义的操作方法
import { SET_TOKEN, SET_USER_INFO, SET_USER_MENU, SET_USER_ROLE } from './types'
import { userRoleApi } from '../../apis/loginApi'

const setTokenAct = token => {
    return {
        type:SET_TOKEN,
        token
    }
}
const setUserInfoAct = userInfo => {
    return {
        type:SET_USER_INFO,
        userInfo
    }
}
const setUserMenuAct = userMenu => {
    return {
        type:SET_USER_MENU,
        userMenu
    }
}
const setUserRoleAct =  roles => {
    // 将异步请求 放在 这里
    return  async dispatch => {
        let  result = await userRoleApi(roles)
        if( result.code === 200 ) {
            dispatch({
                type: SET_USER_ROLE,
                userRole: result.data
            })
        }else{
            dispatch({
                type: SET_USER_ROLE,
                userRole: []
            })
        }
    }
}

export {
    setTokenAct,
    setUserInfoAct,
    setUserMenuAct,
    setUserRoleAct
}